package com.mysoft.gfyx.demo.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.google.gson.JsonObject;
import com.mysoft.framework.rpc.annotation.PubService;
import com.mysoft.framework.rpc.contants.RequestPrefix;
import com.mysoft.gfyx.demo.dto.GetEnterprise2Dto;
import com.mysoft.gfyx.demo.dto.GetEnterpriseDto;
import com.mysoft.gfyx.demo.service.TestMipService;
import com.mysoft.sdk.job.core.api.admin.MysoftJobClient;
import com.mysoft.sdk.job.core.api.admin.MysoftJobInfoDTO;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;

@RestController
@PubService(value = "/test", prefix = RequestPrefix.PUB)
public class TestController extends com.mysoft.framework.mvc.api.Controller {

    @Autowired
    private TestMipService testMipService;

    @Autowired
    private MysoftJobClient mysoftJobClient;


    @RequestMapping("/getEnterprise")
    @ResponseBody
    @Transactional
    public boolean getEnterprise(@RequestBody GetEnterpriseDto dto) {
        testMipService.getEnterpriseInfo(dto);
        return true;
    }

    //查询工商信息测试
    @RequestMapping("/getEnterprise2")
    @ResponseBody
    @Transactional
    public ArrayList getEnterprise2(@RequestBody GetEnterprise2Dto dto) throws JSONException {
        System.out.println("dto1="+ dto);
        LinkedHashMap jsonObject = (LinkedHashMap) testMipService.getEnterpriseInfo2(dto);
        return (ArrayList) jsonObject.get("data");

//        Map maps = (Map) JSON.parse((String) jsonObject);
//        JSONArray data = (JSONArray) maps.get("data");
//        return data;
//
//        System.out.println("结1=");
//        System.out.println(jsonObject);
//        System.out.printf("结果=======jsonObject="+jsonObject.toString());
    }

    @RequestMapping("/startCron")
    @ResponseBody
    public boolean startCron() {
        MysoftJobInfoDTO jobInfoDTO =  new MysoftJobInfoDTO();
        jobInfoDTO.setJobGroupName("gfyx");
        jobInfoDTO.setExecutorHandler("TestJobHandler_tenant_zhoujl");
//        jobInfoDTO.setExecutorHandler("CustomerTimingJobHandler_tenant_zhoujl");
        jobInfoDTO.setJobDesc("客户管理调度");
        jobInfoDTO.setScheduleConf("*/3 * * * * ?");

        Integer jobId = mysoftJobClient.createAndStart(jobInfoDTO);
        mysoftJobClient.update(jobInfoDTO);
        System.out.println(jobId);
        return true;
    }
}